<template>
  <div class="app-container listPage">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同名称" prop="contarctName">
        <el-input
          v-model="queryParams.contarctName"
          placeholder="请输入合同名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contarctCode">
        <el-input
          v-model="queryParams.contarctCode"
          placeholder="请输入合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人名称" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择合同状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['contarct:contarct:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['contarct:contarct:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['contarct:contarct:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['contarct:contarct:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contarctList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column label="合同编号" align="center" prop="contarctCode"/>
      <el-table-column label="租赁编号" align="center" prop="rentCode"/>
      <el-table-column label="物业编号" align="center" prop="propertyCode"/>
      <el-table-column label="合同名称" align="center" prop="contarctName"/>
      <el-table-column label="公司名称" align="center" prop="companyName"/>
      <el-table-column label="通讯地址" align="center" prop="contactAddress"/>
      <el-table-column label="联系人名称" align="center" prop="contactName"/>
      <el-table-column label="联系人电话" align="center" prop="contactMobile"/>
      <el-table-column label="合同开始时间" align="center" prop="contractStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同结束时间" align="center" prop="contractEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房间" align="center" prop="roomNames"/>
      <el-table-column label="签约总面积" align="center" prop="allarea"/>
      <!--      0待签约1已签约待审核2已审核通过3已退租4已过期5已作废-->
      <el-table-column label="合同状态" align="center" prop="contarctStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contarct_status" :value="scope.row.contarctStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="联系人电话" align="center" prop="contactEmail"/>
      <!--      1是按天2是按固定-->
      <el-table-column label="月租赁计算方式" align="center" prop="rentMonthType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rent_month_type" :value="scope.row.rentMonthType"/>
        </template>
      </el-table-column>
      <el-table-column label="租金总价" align="center" prop="rentfee"/>
      <el-table-column label="管理费总价" align="center" prop="propertyfee"/>
      <el-table-column width="130" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >详情
          </el-button>
          <el-button
            v-show="scope.row.contarctStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >修改
          </el-button>
          <el-button
            v-show="scope.row.contarctStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSend(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >提交
          </el-button>
          <el-button
            v-show="scope.row.contarctStatus === 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['contarct:contarct:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同对话框 -->
    <el-dialog title="审核" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审批" prop="contarctStatus">
          <el-radio v-model="form.contarctStatus" :label="2">通过</el-radio>
          <el-radio v-model="form.contarctStatus" :label="1">拒绝</el-radio>
        </el-form-item>
        <el-form-item label="原因" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listContarct,
    getContarct,
    delContarct,
    addContarct,
    updateContarct,
    commitContarct
  } from "@/api/contarct/contarct";

  export default {
    name: "Contarct",
    dicts: ['sys_status', 'contarct_status', 'rent_month_type', 'unit_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 合同表格数据
        contarctList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          contarctCode: null,
          rentCode: null,
          propertyCode: null,
          contarctName: null,
          companyId: null,
          companyName: null,
          contactAddress: null,
          contactName: null,
          contactMobile: null,
          contractStartTime: null,
          contractEndTime: null,
          createTime: null,
          freeStarttime: null,
          freeEndTime: null,
          roomids: null,
          status: null,
          contarctPath: null,
          updateTime: null,
          signBy: null,
          signTime: null,
          bondfee: null,
          bondmouth: null,
          allarea: null,
          contarctStatus: null,
          contactEmail: null,
          rentMonthType: null,
          rentfee: null,
          rentfeeUnit: null,
          rentfeeDay: null,
          rentfeeMonth: null,
          propertyfee: null,
          propertyfeeUnit: null,
          properDay: null,
          properMonth: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    activated() {
      this.getList();
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询合同列表 */
      getList() {
        this.loading = true;
        this.queryParams.contarctStatus = 0
        listContarct(this.queryParams).then(response => {
          this.contarctList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          contarctCode: null,
          rentCode: null,
          propertyCode: null,
          contarctName: null,
          companyId: null,
          companyName: null,
          contactAddress: null,
          contactName: null,
          contactMobile: null,
          contractStartTime: null,
          contractEndTime: null,
          createTime: null,
          freeStarttime: null,
          freeEndTime: null,
          roomids: null,
          status: null,
          contarctPath: null,
          updateTime: null,
          signBy: null,
          signTime: null,
          createBy: null,
          updateBy: null,
          bondfee: null,
          bondmouth: null,
          allarea: null,
          contarctStatus: null,
          contactEmail: null,
          rentMonthType: null,
          rentfee: null,
          rentfeeUnit: null,
          rentfeeDay: null,
          rentfeeMonth: null,
          propertyfee: null,
          propertyfeeUnit: null,
          properDay: null,
          properMonth: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 提交按钮 */
      submitForm() {
        /*this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateContarct(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addContarct(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });*/
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('contarct/contarct/export', {
          ...this.queryParams
        }, `contarct_${new Date().getTime()}.xlsx`)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.conductionData.contractId = undefined
        this.conductionData.updateContractId = undefined
        this.conductionData.contractInfo = {};
        this.$router.push({
          path: "/contarct/ContractNew",
          query: {
            type: "contarctNew"
          }
        });
      },
      //合同详情
      handleDetail(row) {
        this.conductionData.contractId = row.id || this.ids[0]
        console.log("this.conductionData.contractId" + this.conductionData.contractId)
        this.$router.push({
          path: "/contarct/ContarctDetails"
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.conductionData.updateContractId = row.id || this.ids[0]
        this.$router.push({
          path: "/contarct/ContractNew",
          query: {
            type: "contarctUpdate"
          }
        });
      },
      // 提交
      handleSend(row) {
        this.$modal.confirm('是否提交？').then(() => {
          commitContarct(row.id).then(response => {
            console.log(response)
            this.$modal.msgSuccess("提交成功");
            this.getList()
          });
        }).catch(() => {
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除合同编号为"' + ids + '"的数据项？').then(function () {
          return delContarct(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    }
  };
</script>
